package com.apache.bid.controller.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.bid.controller.BaseSupperAction;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidItemGroup;
import com.apache.bid.manager.BidItemGroupManager;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.UnityBaseManager;
import com.apache.cache.util.Validator;

@Controller
@RequestMapping("/admin/group")
public class BidItemGroupAction extends BaseSupperAction<BidItemGroup> {

	@Autowired
	private BidItemGroupManager bidItemGroupManager;
	@Autowired
	private BidItemManager bidItemManager;

	@Override
	protected String getCommBoxValue(BidItemGroup item, String type) {
		if ("id".equals(type)) {
			return item.getGroupId();
		} else {
			return item.getGroupName();
		}
	}

	@Override
	public ResultMsg saveInfo(HttpServletRequest request, HttpServletResponse response, BidItemGroup group) {
		ResultMsg rmsg = new ResultMsg("T", "新增成功！");
		String groupCode = group.getGroupCode();//分组编号不能重复[全局]
		String groupName = group.getGroupName();//分组名称不能重复[同sessionId内]
		if (Validator.isNull(groupCode) || Validator.isNull(groupName)) {
			rmsg.setFlag("F");
			rmsg.setMsg("分组编号和分组名称不能为空！");
			return rmsg;
		}
		String sessionId = group.getSessionId();
		if (Validator.isNull(sessionId)) {
			rmsg.setFlag("F");
			rmsg.setMsg("sessionId不能为空！");
			return rmsg;
		}
		ParamsVo<BidItemGroup> vo = new ParamsVo<BidItemGroup>();
		vo.setMethodKey("checkByCodeOrName");
		vo.setObj(group);
		Map<String, String> resultMap = (Map<String, String>) bidItemGroupManager.execute(vo);
		if ("false".equals(resultMap.get("flag"))) {
			rmsg.setFlag("F");
			rmsg.setMsg(resultMap.get("msg"));
			return rmsg;
		}

		vo = this.getParamsVo(request);
		vo.setObj(group);
		String groupId = bidItemGroupManager.saveInfo(vo);
		if (Validator.isNull(groupId)) {
			return rmsg = new ResultMsg("F", "发生异常！");
		}

		return rmsg;
	}

	/**
	 * 根据id删除分组信息
	 */
	@Override
	public ResultMsg deleteInfo(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {
		//初始化结果提示信息
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		response.setContentType("text/html");
		if (Validator.isNull(id)) {
			rmsg = new ResultMsg("F", "缺少必要的参数!");
			return rmsg;
		}

		//判断组内是否含有标的
		ParamsVo<BidItem> bVo = new ParamsVo<BidItem>();
		BidItem bid = new BidItem();
		bid.setGroupId(id);
		bVo.setObj(bid);
		Long count = bidItemManager.countInfo(bVo);
		//有则，不能删除组信息;无则，执行删除
		if (count.intValue() > 0) {
			rmsg = new ResultMsg("F", "操作失败，分组含有标的数据，不能删除分组信息！");
			return rmsg;
		}

		ParamsVo<BidItemGroup> vo = new ParamsVo<BidItemGroup>();
		vo.setInfoId(id);
		vo.setParams("isDelete", "true");
		boolean mark = bidItemGroupManager.deleteInfo(vo);
		if (!mark) {
			rmsg = new ResultMsg("F", "操作失败！");
		}
		return rmsg;
	}

	protected UnityBaseManager<BidItemGroup> getBaseManager() {
		return bidItemGroupManager;
	}

}
